<!DOCTYPE html>
<html><head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    
    <link rel="alternate" type="application/rss+xml" href="/mybatis/index.xml" title="h-book" />
    <link rel="canonical" href="/mybatis/">

    <title>
        
        Mybatis | h-book
        
    </title>

    
    <link href="/css/fontawesome.min.css" rel="stylesheet">

    
    <link rel="stylesheet" href="/css/ace.min.css">

    

    

</head>
<body><nav class="navbar navbar-expand-lg navbar-dark bg-primary shadow sticky-top" id="navbarMain">
    <div class="container">
        <div>
            <a class="navbar-brand" href="/">
                
                h-book
            </a>
        </div>
    </div>
</nav>
<div class="container-fluid">
            <div class="row">

                <div class="docs-sidenav order-0 col-12 col-md-3 col-lg-2 col-xl-2 position-sticky border-right"><nav class="navbar navbar-expand-md navbar-light pl-0">
    <button class="navbar-toggler navbar-toggler-right collapsed" type="button" data-toggle="collapse" data-target="#sidenav-left-collapse" aria-controls="sidenav-left-collapse" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

     <div class="collapse navbar-collapse align-items-start flex-column" id="sidenav-left-collapse">
            <form class="form-inline my-2 my-lg-0 searchbox">
                <input class="form-control mr-sm-2 w-100" data-search-input id="search-by" type="text" placeholder="Search">
            </form>

        

         <ul class="navbar-nav flex-column pt-3">
    <li data-nav-id="/hugo/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/hugo/"><h6>Hugo</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/hugo/init/" class="nav-item my-1">
                
                
                  <a href="/hugo/init/" class="nav-link p-0">
                    init
                  </a>
                
        </li>
        <li data-nav-id="/hugo/add-page/" class="nav-item my-1">
                
                
                  <a href="/hugo/add-page/" class="nav-link p-0">
                    add-page
                  </a>
                
        </li>
        <li data-nav-id="/hugo/edit-skill/" class="nav-item my-1">
                
                
                  <a href="/hugo/edit-skill/" class="nav-link p-0">
                    edit-skill
                  </a>
                
        </li>
        <li data-nav-id="/hugo/publish/" class="nav-item my-1">
                
                
                  <a href="/hugo/publish/" class="nav-link p-0">
                    publish
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/android/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/android/"><h6>Android</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/android/install/" class="nav-item my-1">
                
                
                  <a href="/android/install/" class="nav-link p-0">
                    install
                  </a>
                
        </li>
        <li data-nav-id="/android/create-project/" class="nav-item my-1">
                
                
                  <a href="/android/create-project/" class="nav-link p-0">
                    create project
                  </a>
                
        </li>
        <li data-nav-id="/android/import-material/" class="nav-item my-1">
                
                
                  <a href="/android/import-material/" class="nav-link p-0">
                    import material
                  </a>
                
        </li>
        <li data-nav-id="/android/start-page/" class="nav-item my-1">
                
                
                  <a href="/android/start-page/" class="nav-link p-0">
                    startpage
                  </a>
                
        </li>
        <li data-nav-id="/android/i18n/" class="nav-item my-1">
                
                
                  <a href="/android/i18n/" class="nav-link p-0">
                    i18n
                  </a>
                
        </li>
        <li data-nav-id="/android/nav-flat-page/" class="nav-item my-1">
                
                
                  <a href="/android/nav-flat-page/" class="nav-link p-0">
                    nav flat page
                  </a>
                
        </li>
        <li data-nav-id="/android/activity/" class="nav-item my-1">
                
                
                  <a href="/android/activity/" class="nav-link p-0">
                    Activity
                  </a>
                
        </li>
        <li data-nav-id="/android/db/" class="nav-item my-1">
                
                
                  <a href="/android/db/" class="nav-link p-0">
                    DB
                  </a>
                
        </li>
        <li data-nav-id="/android/dialog/" class="nav-item my-1">
                
                
                  <a href="/android/dialog/" class="nav-link p-0">
                    Dialog
                  </a>
                
        </li>
        <li data-nav-id="/android/fragment/" class="nav-item my-1">
                
                
                  <a href="/android/fragment/" class="nav-link p-0">
                    Fragment
                  </a>
                
        </li>
        <li data-nav-id="/android/jetpack/" class="nav-item my-1">
                
                
                  <a href="/android/jetpack/" class="nav-link p-0">
                    Jetpack
                  </a>
                
        </li>
        <li data-nav-id="/android/material/" class="nav-item my-1">
                
                
                  <a href="/android/material/" class="nav-link p-0">
                    Material
                  </a>
                
        </li>
        <li data-nav-id="/android/storage/" class="nav-item my-1">
                
                
                  <a href="/android/storage/" class="nav-link p-0">
                    Storage
                  </a>
                
        </li>
        <li data-nav-id="/android/%E5%9B%BE%E6%A0%87/" class="nav-item my-1">
                
                
                  <a href="/android/%E5%9B%BE%E6%A0%87/" class="nav-link p-0">
                    图标
                  </a>
                
        </li>
        <li data-nav-id="/android/store/" class="nav-item my-1">
                
                
                  <a href="/android/store/" class="nav-link p-0">
                    Store
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/blender/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/blender/"><h6>Blender</h6></a>
        
    </li>
    <li data-nav-id="/bootstrap/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/bootstrap/"><h6>BootStrap</h6></a>
        
    </li>
    <li data-nav-id="/centos7/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/centos7/"><h6>Centos7</h6></a>
        
    </li>
    <li data-nav-id="/dlna/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/dlna/"><h6>DLNA</h6></a>
        
    </li>
    <li data-nav-id="/docker/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/docker/"><h6>Docker</h6></a>
        
    </li>
    <li data-nav-id="/dubbo/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/dubbo/"><h6>Dubbo</h6></a>
        
    </li>
    <li data-nav-id="/ffmpeg/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/ffmpeg/"><h6>FFmpeg</h6></a>
        
    </li>
    <li data-nav-id="/git/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/git/"><h6>Git</h6></a>
        
    </li>
    <li data-nav-id="/gitbook/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/gitbook/"><h6>Gitbook</h6></a>
        
    </li>
    <li data-nav-id="/gitlab/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/gitlab/"><h6>Gitlab</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/gitlab/runner/" class="nav-item my-1">
                
                
                  <a href="/gitlab/runner/" class="nav-link p-0">
                    Runner
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/gradle/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/gradle/"><h6>Gradle</h6></a>
        
    </li>
    <li data-nav-id="/java/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/java/"><h6>Java</h6></a>
        
    </li>
    <li data-nav-id="/jenkins/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/jenkins/"><h6>Jenkins</h6></a>
        
    </li>
    <li data-nav-id="/kubernetes/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/kubernetes/"><h6>Kubernetes</h6></a>
        
    </li>
    <li data-nav-id="/mybatis/" class="nav-item my-1 active
        ">
        
        
          <a class="nav-link p-0" href="/mybatis/"><h6>Mybatis</h6></a>
        
    </li>
    <li data-nav-id="/mybatis-plus/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/mybatis-plus/"><h6>Mybatis Plus</h6></a>
        
    </li>
    <li data-nav-id="/mysql/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/mysql/"><h6>MySQL</h6></a>
        
    </li>
    <li data-nav-id="/nodejs/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/nodejs/"><h6>Node.js</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/nodejs/npm/" class="nav-item my-1">
                
                
                  <a href="/nodejs/npm/" class="nav-link p-0">
                    NPM
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/postgresql/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/postgresql/"><h6>PostgreSQL</h6></a>
        
    </li>
    <li data-nav-id="/redis/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/redis/"><h6>Redis</h6></a>
        
    </li>
    <li data-nav-id="/samba/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/samba/"><h6>Samba</h6></a>
        
    </li>
    <li data-nav-id="/sql/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/sql/"><h6>SQL</h6></a>
        
    </li>
    <li data-nav-id="/ue5/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/ue5/"><h6>UE5</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/ue5/install/" class="nav-item my-1">
                
                
                  <a href="/ue5/install/" class="nav-link p-0">
                    install
                  </a>
                
        </li>
        <li data-nav-id="/ue5/uninstall/" class="nav-item my-1">
                
                
                  <a href="/ue5/uninstall/" class="nav-link p-0">
                    uninstall
                  </a>
                
        </li>
        <li data-nav-id="/ue5/qa/" class="nav-item my-1">
                
                
                  <a href="/ue5/qa/" class="nav-link p-0">
                    Q&amp;A
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity/"><h6>Unity</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity/install/" class="nav-item my-1">
                
                
                  <a href="/unity/install/" class="nav-link p-0">
                    install
                  </a>
                
        </li>
        <li data-nav-id="/unity/scene/" class="nav-item my-1">
                
                
                  <a href="/unity/scene/" class="nav-link p-0">
                    scene
                  </a>
                
        </li>
        <li data-nav-id="/unity/inspector/" class="nav-item my-1">
                
                
                  <a href="/unity/inspector/" class="nav-link p-0">
                    inspector
                  </a>
                
        </li>
        <li data-nav-id="/unity/assets/" class="nav-item my-1">
                
                
                  <a href="/unity/assets/" class="nav-link p-0">
                    assets
                  </a>
                
        </li>
        <li data-nav-id="/unity/audio-source/" class="nav-item my-1">
                
                
                  <a href="/unity/audio-source/" class="nav-link p-0">
                    audio source
                  </a>
                
        </li>
        <li data-nav-id="/unity/camera/" class="nav-item my-1">
                
                
                  <a href="/unity/camera/" class="nav-link p-0">
                    camera
                  </a>
                
        </li>
        <li data-nav-id="/unity/script/" class="nav-item my-1">
                
                
                  <a href="/unity/script/" class="nav-link p-0">
                    script
                  </a>
                
        </li>
        <li data-nav-id="/unity/prefabs/" class="nav-item my-1">
                
                
                  <a href="/unity/prefabs/" class="nav-link p-0">
                    prefabs
                  </a>
                
        </li>
        <li data-nav-id="/unity/physics/" class="nav-item my-1">
                
                
                  <a href="/unity/physics/" class="nav-link p-0">
                    physics
                  </a>
                
        </li>
        <li data-nav-id="/unity/skybox/" class="nav-item my-1">
                
                
                  <a href="/unity/skybox/" class="nav-link p-0">
                    skybox
                  </a>
                
        </li>
        <li data-nav-id="/unity/particle-system/" class="nav-item my-1">
                
                
                  <a href="/unity/particle-system/" class="nav-link p-0">
                    particle system
                  </a>
                
        </li>
        <li data-nav-id="/unity/shortcut-keys/" class="nav-item my-1">
                
                
                  <a href="/unity/shortcut-keys/" class="nav-link p-0">
                    shortcut keys
                  </a>
                
        </li>
        <li data-nav-id="/unity/qa/" class="nav-item my-1">
                
                
                  <a href="/unity/qa/" class="nav-link p-0">
                    Q&amp;A
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-2d-beginner/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-2d-beginner/"><h6>Unity 2D Beginner</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-2d-beginner/init-and-move/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/init-and-move/" class="nav-link p-0">
                    Init &amp; Move
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/map/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/map/" class="nav-link p-0">
                    Map
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/data/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/data/" class="nav-link p-0">
                    Data
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/animate/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/animate/" class="nav-link p-0">
                    Data
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/bullet/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/bullet/" class="nav-link p-0">
                    Bullet
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/cinemachine/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/cinemachine/" class="nav-link p-0">
                    Cinemachine
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/particle/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/particle/" class="nav-link p-0">
                    Particle
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/ui/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/ui/" class="nav-link p-0">
                    UI
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/npc/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/npc/" class="nav-link p-0">
                    NPC
                  </a>
                
        </li>
        <li data-nav-id="/unity-2d-beginner/sound/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-beginner/sound/" class="nav-link p-0">
                    Sound
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-2d-game-kit/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-2d-game-kit/"><h6>Unity 2D Game Kit</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-2d-game-kit/record/" class="nav-item my-1">
                
                
                  <a href="/unity-2d-game-kit/record/" class="nav-link p-0">
                    record
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-3d-beginner/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-3d-beginner/"><h6>Unity 3D Beginner</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-3d-beginner/init/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/init/" class="nav-link p-0">
                    Init
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/level/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/level/" class="nav-link p-0">
                    Level
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/nav/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/nav/" class="nav-link p-0">
                    Nav
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/render-pipeline/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/render-pipeline/" class="nav-link p-0">
                    Render Pipeline
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/postprocessing/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/postprocessing/" class="nav-link p-0">
                    PostProcessing
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/ui/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/ui/" class="nav-link p-0">
                    UI
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/enemy/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/enemy/" class="nav-link p-0">
                    Enemy
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/sound/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/sound/" class="nav-link p-0">
                    Sound
                  </a>
                
        </li>
        <li data-nav-id="/unity-3d-beginner/%E5%BB%BA%E6%A8%A1%E5%B7%A5%E5%85%B7/" class="nav-item my-1">
                
                
                  <a href="/unity-3d-beginner/%E5%BB%BA%E6%A8%A1%E5%B7%A5%E5%85%B7/" class="nav-link p-0">
                    建模工具
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-arpg/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-arpg/"><h6>Unity ARPG</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-arpg/init/" class="nav-item my-1">
                
                
                  <a href="/unity-arpg/init/" class="nav-link p-0">
                    Init
                  </a>
                
        </li>
        <li data-nav-id="/unity-arpg/role-state-machine/" class="nav-item my-1">
                
                
                  <a href="/unity-arpg/role-state-machine/" class="nav-link p-0">
                    Role State Machine
                  </a>
                
        </li>
        <li data-nav-id="/unity-arpg/api-piece/" class="nav-item my-1">
                
                
                  <a href="/unity-arpg/api-piece/" class="nav-link p-0">
                    API Piece
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-creativecore-animation/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-creativecore-animation/"><h6>Unity Creativecore Animation</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-creativecore-animation/door/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-animation/door/" class="nav-link p-0">
                    Door
                  </a>
                
        </li>
        <li data-nav-id="/unity-creativecore-animation/init/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-animation/init/" class="nav-link p-0">
                    Init
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-creativecore-lighting/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-creativecore-lighting/"><h6>Unity Creativecore Lighting</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-creativecore-lighting/init/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-lighting/init/" class="nav-link p-0">
                    Init
                  </a>
                
        </li>
        <li data-nav-id="/unity-creativecore-lighting/outdoor/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-lighting/outdoor/" class="nav-link p-0">
                    Outdoor
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-creativecore-shader/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-creativecore-shader/"><h6>Unity Creativecore Shader</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-creativecore-shader/init/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-shader/init/" class="nav-link p-0">
                    Init
                  </a>
                
        </li>
        <li data-nav-id="/unity-creativecore-shader/glass/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-shader/glass/" class="nav-link p-0">
                    Glass
                  </a>
                
        </li>
        <li data-nav-id="/unity-creativecore-shader/shader-graph/" class="nav-item my-1">
                
                
                  <a href="/unity-creativecore-shader/shader-graph/" class="nav-link p-0">
                    Shader-Graph
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-creator-kit-beginner-code/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-creator-kit-beginner-code/"><h6>Unity Creator Kit Beginner Code</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-creator-kit-beginner-code/record/" class="nav-item my-1">
                
                
                  <a href="/unity-creator-kit-beginner-code/record/" class="nav-link p-0">
                    record
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-creator-kit-fps/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-creator-kit-fps/"><h6>Unity Creator Kit FPS</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-creator-kit-fps/record/" class="nav-item my-1">
                
                
                  <a href="/unity-creator-kit-fps/record/" class="nav-link p-0">
                    record
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-mythirdperson/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-mythirdperson/"><h6>Unity My Third Person</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-mythirdperson/init/" class="nav-item my-1">
                
                
                  <a href="/unity-mythirdperson/init/" class="nav-link p-0">
                    Init
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-playground/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-playground/"><h6>Unity Playground</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-playground/record/" class="nav-item my-1">
                
                
                  <a href="/unity-playground/record/" class="nav-link p-0">
                    record
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-ui/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-ui/"><h6>Unity UI</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-ui/init/" class="nav-item my-1">
                
                
                  <a href="/unity-ui/init/" class="nav-link p-0">
                    record
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/unity-vfx/" class="nav-item my-1 haschildren
        ">
        
        
          <a class="nav-link p-0" href="/unity-vfx/"><h6>Unity VFX</h6></a>
        
        <ul class="list-unstyled ml-2">
        <li data-nav-id="/unity-vfx/init/" class="nav-item my-1">
                
                
                  <a href="/unity-vfx/init/" class="nav-link p-0">
                    record
                  </a>
                
        </li>
        <li data-nav-id="/unity-vfx/bullet/" class="nav-item my-1">
                
                
                  <a href="/unity-vfx/bullet/" class="nav-link p-0">
                    bullet
                  </a>
                
        </li>
        <li data-nav-id="/unity-vfx/rain/" class="nav-item my-1">
                
                
                  <a href="/unity-vfx/rain/" class="nav-link p-0">
                    rain
                  </a>
                
        </li>
        <li data-nav-id="/unity-vfx/rock-rain/" class="nav-item my-1">
                
                
                  <a href="/unity-vfx/rock-rain/" class="nav-link p-0">
                    Rock Rain
                  </a>
                
        </li>
        </ul>
    </li>
    <li data-nav-id="/vue/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/vue/"><h6>Vue</h6></a>
        
    </li>
    <li data-nav-id="/web/" class="nav-item my-1
        ">
        
        
          <a class="nav-link p-0" href="/web/"><h6>Web</h6></a>
        
    </li>
        </ul>
    </div>
</nav>


</div>
                <div class="docs-toc large order-lg-2 order-md-0 order-xs-1 col-12 col-lg-2 col-xl-2 position-sticky border-left"><div class="docs-toc">
	<nav id="TableOfContents">
  <ul>
    <li><a href="#依赖">依赖</a></li>
    <li><a href="#配置文件">配置文件</a></li>
    <li><a href="#简单使用">简单使用</a></li>
    <li><a href="#select">Select</a></li>
    <li><a href="#insert">insert</a></li>
    <li><a href="#update">update</a></li>
    <li><a href="#delete">delete</a></li>
    <li><a href="#生命周期和作用域">生命周期和作用域</a></li>
    <li><a href="#resultmap">resultMap</a></li>
    <li><a href="#分页">分页</a></li>
    <li><a href="#注解">注解</a></li>
    <li><a href="#流程">流程</a></li>
    <li><a href="#-"><code>#{} ${}</code></a></li>
    <li><a href="#多对一">多对一</a>
      <ul>
        <li><a href="#方法一-子查询">方法一 子查询</a></li>
        <li><a href="#方法二-连表查询">方法二 连表查询</a></li>
      </ul>
    </li>
    <li><a href="#一对多">一对多</a>
      <ul>
        <li><a href="#方法一-连表查询">方法一 连表查询</a></li>
        <li><a href="#方法二-子查询">方法二 子查询</a></li>
      </ul>
    </li>
    <li><a href="#if">if</a></li>
    <li><a href="#choose-when-otherwise">choose when otherwise</a></li>
    <li><a href="#set">set</a></li>
    <li><a href="#trim">trim</a></li>
    <li><a href="#sql片段">sql片段</a></li>
    <li><a href="#foreach">foreach</a></li>
    <li><a href="#缓存">缓存</a></li>
  </ul>
</nav>
</div>
</div>
                <div class="main col-12 order-1 col-md-9 col-lg-10 col-xl-8 py-3">
                

<div class="d-flex flex-column">
    <h1 class="js-title">Mybatis</h1>
    <div class="d-flex align-items-center">
        
    </div>
</div>

<hr>


<h2 id="依赖">依赖</h2>
<p>原生的参考官网，或者结合Spring Boot使用&quot;com.baomidou:mybatis-plus-boot-starter:x.y.z&quot;</p>
<h2 id="配置文件">配置文件</h2>
<p>如果使用结合Spring Boot的，已经自带配置，可以覆盖定义</p>
<p>mybatis-config.xml xml头参看官网，文件放在resources文件夹下</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;settings&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;setting</span> <span style="color:#75af00">name=</span><span style="color:#d88200">&#34;logImpl&#34;</span> <span style="color:#75af00">value=</span><span style="color:#d88200">&#34;&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/settings&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;typeAliases&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;typeAlias</span> <span style="color:#75af00">alias=</span><span style="color:#d88200">&#34;User&#34;</span> <span style="color:#960050;background-color:#1e0010">type&#34;a.b.c.User&#34;</span><span style="color:#f92672">&gt;&lt;/typeAlias&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;package</span> <span style="color:#75af00">name=</span><span style="color:#d88200">&#34;a.b.c&#34;</span><span style="color:#f92672">&gt;&lt;/package&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/typeAliases&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;environments</span> <span style="color:#75af00">default=</span><span style="color:#d88200">&#34;a&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;environment</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;a&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;transactionManager</span> <span style="color:#75af00">type=</span><span style="color:#d88200">&#34;JDBC&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;dataSource</span> <span style="color:#75af00">type=</span><span style="color:#d88200">&#34;POOLED&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">&lt;property</span> <span style="color:#75af00">name=</span><span style="color:#d88200">&#34;driver&#34;</span> <span style="color:#75af00">value=</span><span style="color:#d88200">&#34;com.mysql.jdbc.Driver&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>                   ...url...?useUnicode=true<span style="color:#111">&amp;amp;</span>characterEncoding=UTF-8
</span></span><span style="display:flex;"><span>                   ...username...
</span></span><span style="display:flex;"><span>                   ...password...
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;/dataSource&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/environment&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;environment</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;b&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    ...
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/environment&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/environments&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;mappers&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;mapper</span> <span style="color:#75af00">resource=</span><span style="color:#d88200">&#34;a/b/c/mapper.xml&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/mappers&gt;</span>
</span></span></code></pre></div><p>setting logImpl value 可填SLF4J | LOG4J | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | STDOUT_LOGGING | NO_LOGGING 用于输出日志</p>
<p>用typeAliases可缩短类名；也可以指定一个包名，此时别名为类名，但小写打头，如果要自定义就在类上@Alias(&ldquo;xxx&rdquo;)</p>
<p>transactionManager type=&ldquo;JDBC&rdquo;|&ldquo;MANAGED&rdquo; 前者依赖数据源得到的连接来管理事务;后者几乎没做什么（适用范围很小，参看官网）。如果用spring + mybatis则不需要管这个配置</p>
<p>dataSource type=&ldquo;POOLED&rdquo;|&ldquo;UNPOOLED&rdquo;|&ldquo;JNDI&rdquo; 前者连接池；中间无连接池；后者EJB用的</p>
<p>mapper resource|class 推荐使用resource；class接口和配置文件必须同名且在同包下</p>
<h2 id="简单使用">简单使用</h2>
<p>transactionManager默认的JDBC的事务管理</p>
<p>每个基于Mybatis的应用都是一个SqlSessionFactory的实例为核心</p>
<p>SqlSessionFactory的实例可以通过SqlSessionFactoryBuilder创建</p>
<p>从SqlSessionFactory中可以获取SqlSession实例</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#111">String</span> <span style="color:#111">resource</span> <span style="color:#f92672">=</span> <span style="color:#d88200">&#34;.../mybatis-config.xml&#34;</span><span style="color:#111">;</span>
</span></span><span style="display:flex;"><span><span style="color:#111">InputStream</span> <span style="color:#111">inputStream</span> <span style="color:#f92672">=</span> <span style="color:#111">Resources</span><span style="color:#111">.</span><span style="color:#75af00">getResourceAsStream</span><span style="color:#111">(</span><span style="color:#111">resource</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#111">SqlSessionFactory</span> <span style="color:#111">ssf</span> <span style="color:#f92672">=</span> <span style="color:#00a8c8">new</span> <span style="color:#111">SqlSessionFactoryBuilder</span><span style="color:#111">().</span><span style="color:#75af00">build</span><span style="color:#111">(</span><span style="color:#111">inputStream</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">try</span> <span style="color:#111">(</span><span style="color:#111">SqlSession</span> <span style="color:#111">session</span> <span style="color:#f92672">=</span> <span style="color:#111">ssf</span><span style="color:#111">.</span><span style="color:#75af00">openSession</span><span style="color:#111">())</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">BlogMapper</span> <span style="color:#111">mapper</span> <span style="color:#f92672">=</span> <span style="color:#111">session</span><span style="color:#111">.</span><span style="color:#75af00">getMapper</span><span style="color:#111">(</span><span style="color:#111">BlogMapper</span><span style="color:#111">.</span><span style="color:#75af00">class</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>    <span style="color:#111">Blog</span> <span style="color:#111">blog</span> <span style="color:#f92672">=</span> <span style="color:#111">mapper</span><span style="color:#111">.</span><span style="color:#75af00">selectBlog</span><span style="color:#111">(</span><span style="color:#111">101</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span></code></pre></div><p>mapper.xml xml头参看官网，文件放在BlogMapper同目录下（如果maven工程，需要在<code>&lt;build&gt;</code>中加<code>&lt;resource&gt;</code>）</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;resource&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;directory&gt;</span>src/main/java<span style="color:#f92672">&lt;/directory&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;includes&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;include&gt;</span>**/*.xml<span style="color:#f92672">&lt;/include&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/includes&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;filtering&gt;</span>true<span style="color:#f92672">&lt;/filtering&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/resource&gt;</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;mapper</span> <span style="color:#75af00">namespace=</span><span style="color:#d88200">&#34;a.b.c.BlogMapper&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;select</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;selectBlog&#34;</span> <span style="color:#75af00">resultType=</span><span style="color:#d88200">&#34;e.f.g.Blog&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    select * from Blog where id = ${id}
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/select&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/mapper&gt;</span>
</span></span></code></pre></div><p>id对应BlogMapper中的方法名</p>
<p>resultType对应定义的模型对象（DO / PO）</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#00a8c8">public</span> <span style="color:#00a8c8">interface</span> <span style="color:#75af00">BlogMapper</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>  <span style="color:#111">Blog</span> <span style="color:#75af00">selectBlog</span><span style="color:#111">(</span><span style="color:#00a8c8">int</span> <span style="color:#111">id</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span></code></pre></div><p>SqlSession实例不是线程安全的，每次一个请求就开一个，然后关闭</p>
<h2 id="select">Select</h2>
<ul>
<li>id 方法名</li>
<li>resultType 返回值类型</li>
<li>parameterType 入参类型</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#00a8c8">public</span> <span style="color:#00a8c8">interface</span> <span style="color:#75af00">UserMapper</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>  <span style="color:#111">User</span> <span style="color:#75af00">getUserById</span><span style="color:#111">(</span><span style="color:#00a8c8">int</span> <span style="color:#111">id</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>  <span style="color:#111">User</span> <span style="color:#75af00">getUserLikeByName</span><span style="color:#111">(</span><span style="color:#111">String</span> <span style="color:#111">name</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;getUserById&#34;</span> <span style="color:#75af00">parameterType=</span><span style="color:#d88200">&#34;int&#34;</span> <span style="color:#75af00">resultType=</span><span style="color:#d88200">&#34;User&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  select * from user where id = #{id}
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/select&gt;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;getUserLike&#34;</span> <span style="color:#75af00">resultType=</span><span style="color:#d88200">&#34;User&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  select * from user where name like #{value}
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/select&gt;</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#111">userMapper</span><span style="color:#111">.</span><span style="color:#75af00">getUserLike</span><span style="color:#111">(</span><span style="color:#d88200">&#34;%王%&#34;</span><span style="color:#111">);</span> <span style="color:#75715e">// 防SQL注入</span>
</span></span></code></pre></div><h2 id="insert">insert</h2>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#00a8c8">public</span> <span style="color:#00a8c8">interface</span> <span style="color:#75af00">UserMapper</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>  <span style="color:#00a8c8">int</span> <span style="color:#75af00">addUser</span><span style="color:#111">(</span><span style="color:#111">User</span> <span style="color:#111">user</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;insert</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;addUser&#34;</span> <span style="color:#75af00">parameterType=</span><span style="color:#d88200">&#34;User&#34;</span> <span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  insert into user (id, name, pwd) values (#{id}, #{name} #{pwd})
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/select&gt;</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#111">User</span> <span style="color:#111">user</span> <span style="color:#f92672">=</span> <span style="color:#00a8c8">new</span> <span style="color:#111">User</span><span style="color:#111">();</span>
</span></span><span style="display:flex;"><span><span style="color:#111">userMapper</span><span style="color:#111">.</span><span style="color:#75af00">addUser</span><span style="color:#111">(</span><span style="color:#111">user</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#111">sqlSession</span><span style="color:#111">.</span><span style="color:#75af00">commit</span><span style="color:#111">();</span>
</span></span></code></pre></div><h2 id="update">update</h2>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#00a8c8">public</span> <span style="color:#00a8c8">interface</span> <span style="color:#75af00">UserMapper</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>  <span style="color:#00a8c8">int</span> <span style="color:#75af00">updatedUser</span><span style="color:#111">(</span><span style="color:#111">User</span> <span style="color:#111">user</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;update</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;updatedUser&#34;</span> <span style="color:#75af00">parameterType=</span><span style="color:#d88200">&#34;User&#34;</span> <span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  update user set name = #{name}, pwd = #{pwd} where id = #{id}
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/update&gt;</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#111">User</span> <span style="color:#111">user</span> <span style="color:#f92672">=</span> <span style="color:#00a8c8">new</span> <span style="color:#111">User</span><span style="color:#111">();</span>
</span></span><span style="display:flex;"><span><span style="color:#111">userMapper</span><span style="color:#111">.</span><span style="color:#75af00">updatedUser</span><span style="color:#111">(</span><span style="color:#111">user</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#111">sqlSession</span><span style="color:#111">.</span><span style="color:#75af00">commit</span><span style="color:#111">();</span>
</span></span></code></pre></div><h2 id="delete">delete</h2>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#00a8c8">public</span> <span style="color:#00a8c8">interface</span> <span style="color:#75af00">UserMapper</span> <span style="color:#111">{</span>
</span></span><span style="display:flex;"><span>  <span style="color:#00a8c8">int</span> <span style="color:#75af00">deleteUserById</span><span style="color:#111">(</span><span style="color:#00a8c8">int</span> <span style="color:#111">id</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#111">}</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;delete</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;deleteUserById&#34;</span> <span style="color:#75af00">parameterType=</span><span style="color:#d88200">&#34;int&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  delete from user where id = #{id}
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/delete&gt;</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#111">userMapper</span><span style="color:#111">.</span><span style="color:#75af00">deleteUserById</span><span style="color:#111">(</span><span style="color:#111">1</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#111">sqlSession</span><span style="color:#111">.</span><span style="color:#75af00">commit</span><span style="color:#111">();</span>
</span></span></code></pre></div><h2 id="生命周期和作用域">生命周期和作用域</h2>
<p>SqlSessionFactoryBuilder一旦创建SqlSessionFactory就不需要了，所以局部变量即可</p>
<p>SqlSessionFactory类同于数据库连接池，一旦创建则一直存在，所以是全局变量（单例）</p>
<p>SqlSession类同于连接到池的申请请求，线程不安全，用完就关闭</p>
<h2 id="resultmap">resultMap</h2>
<p>处理数据模型和表列名不同的情况，相同的可以不写，如下id和name可以删掉</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;resultMap</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;UserMap&#34;</span> <span style="color:#75af00">type=</span><span style="color:#d88200">&#34;User&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;result</span> <span style="color:#75af00">column=</span><span style="color:#d88200">&#34;id&#34;</span> <span style="color:#75af00">property=</span><span style="color:#d88200">&#34;id&#34;</span><span style="color:#f92672">&gt;&lt;/result&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;result</span> <span style="color:#75af00">column=</span><span style="color:#d88200">&#34;name&#34;</span> <span style="color:#75af00">property=</span><span style="color:#d88200">&#34;name&#34;</span><span style="color:#f92672">&gt;&lt;/result&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;result</span> <span style="color:#75af00">column=</span><span style="color:#d88200">&#34;pwd&#34;</span> <span style="color:#75af00">property=</span><span style="color:#d88200">&#34;password&#34;</span><span style="color:#f92672">&gt;&lt;/result&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/resultMap&gt;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;getUserById&#34;</span> <span style="color:#75af00">resultMap=</span><span style="color:#d88200">&#34;UserMap&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  select * from user where id = #{id}
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/delete&gt;</span>
</span></span></code></pre></div><h2 id="分页">分页</h2>
<ul>
<li>limit sql</li>
<li>RowBounds sqlSession.selectList其中的重载 传入RowBouns(x,y) 不建议使用</li>
<li>PageHelper 插件</li>
</ul>
<h2 id="注解">注解</h2>
<p>接口方法上使用@Select(&quot;<code>&lt;sql&gt;</code>&quot;)注解（@Insert、@Update、@Delete），传参用@Param()，配置文件写</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;mappers&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;mapper</span> <span style="color:#75af00">class=</span><span style="color:#d88200">&#34;mapper的路径&#34;</span><span style="color:#f92672">&gt;&lt;/mapper&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/mappers&gt;</span>
</span></span></code></pre></div><p>复杂逻辑就不建议使用了</p>
<h2 id="流程">流程</h2>
<ul>
<li>Resources读取配置文件</li>
<li>SqlSessionFactoryBuilder实例化</li>
<li>XMLConfigBuilder解配置文件</li>
<li>Configuration存所有配置</li>
<li>SqlSessionFactory实例化</li>
<li>transactional事务管理</li>
<li>executor执行器</li>
<li>SqlSession实例化</li>
<li>CRUD</li>
</ul>
<h2 id="-"><code>#{} ${}</code></h2>
<p><code>${}</code>会有SQL的问题</p>
<h2 id="多对一">多对一</h2>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sql" data-lang="sql"><span style="display:flex;"><span><span style="color:#00a8c8">CREATE</span> <span style="color:#00a8c8">TABLE</span> <span style="color:#f92672">`</span><span style="color:#111">teacher</span><span style="color:#f92672">`</span> <span style="color:#111">(</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">`</span><span style="color:#111">id</span><span style="color:#f92672">`</span> <span style="color:#111">INT</span><span style="color:#111">(</span><span style="color:#ae81ff">10</span><span style="color:#111">)</span> <span style="color:#00a8c8">NOT</span> <span style="color:#00a8c8">NULL</span><span style="color:#111">,</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">`</span><span style="color:#111">name</span><span style="color:#f92672">`</span> <span style="color:#111">VARCHAR</span><span style="color:#111">(</span><span style="color:#ae81ff">30</span><span style="color:#111">)</span> <span style="color:#00a8c8">DEFAULT</span> <span style="color:#00a8c8">NULL</span><span style="color:#111">,</span>
</span></span><span style="display:flex;"><span>  <span style="color:#00a8c8">PRIMARY</span> <span style="color:#00a8c8">KEY</span> <span style="color:#111">(</span><span style="color:#f92672">`</span><span style="color:#111">id</span><span style="color:#f92672">`</span><span style="color:#111">)</span>
</span></span><span style="display:flex;"><span><span style="color:#111">)</span> <span style="color:#111">ENGINE</span><span style="color:#f92672">=</span><span style="color:#111">INNODB</span> <span style="color:#00a8c8">DEFAULT</span> <span style="color:#111">CHARSET</span><span style="color:#f92672">=</span><span style="color:#111">utf8</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">INSERT</span> <span style="color:#00a8c8">INTO</span> <span style="color:#111">teacher</span><span style="color:#111">(</span><span style="color:#f92672">`</span><span style="color:#111">id</span><span style="color:#f92672">`</span><span style="color:#111">,</span> <span style="color:#f92672">`</span><span style="color:#111">name</span><span style="color:#f92672">`</span><span style="color:#111">)</span> <span style="color:#00a8c8">VALUES</span> <span style="color:#111">(</span><span style="color:#ae81ff">1</span><span style="color:#111">,</span> <span style="color:#d88200">&#39;老师&#39;</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">CREATE</span> <span style="color:#00a8c8">TABLE</span> <span style="color:#f92672">`</span><span style="color:#111">student</span><span style="color:#f92672">`</span> <span style="color:#111">(</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">`</span><span style="color:#111">id</span><span style="color:#f92672">`</span> <span style="color:#111">INT</span><span style="color:#111">(</span><span style="color:#ae81ff">10</span><span style="color:#111">)</span> <span style="color:#00a8c8">NOT</span> <span style="color:#00a8c8">NULL</span><span style="color:#111">,</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">`</span><span style="color:#111">name</span><span style="color:#f92672">`</span> <span style="color:#111">VARCHAR</span><span style="color:#111">(</span><span style="color:#ae81ff">30</span><span style="color:#111">)</span> <span style="color:#00a8c8">DEFAULT</span> <span style="color:#00a8c8">NULL</span><span style="color:#111">,</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">`</span><span style="color:#111">tid</span><span style="color:#f92672">`</span> <span style="color:#111">INT</span><span style="color:#111">(</span><span style="color:#ae81ff">10</span><span style="color:#111">)</span> <span style="color:#00a8c8">DEFAULT</span> <span style="color:#00a8c8">NULL</span><span style="color:#111">,</span>
</span></span><span style="display:flex;"><span>  <span style="color:#00a8c8">PRIMARY</span> <span style="color:#00a8c8">KEY</span> <span style="color:#111">(</span><span style="color:#f92672">`</span><span style="color:#111">id</span><span style="color:#f92672">`</span><span style="color:#111">),</span>
</span></span><span style="display:flex;"><span>  <span style="color:#00a8c8">KEY</span> <span style="color:#f92672">`</span><span style="color:#111">fktid</span><span style="color:#f92672">`</span> <span style="color:#111">(</span><span style="color:#f92672">`</span><span style="color:#111">tid</span><span style="color:#f92672">`</span><span style="color:#111">),</span>
</span></span><span style="display:flex;"><span>  <span style="color:#00a8c8">CONSTRAINT</span> <span style="color:#f92672">`</span><span style="color:#111">fktid</span><span style="color:#f92672">`</span> <span style="color:#00a8c8">FOREIGN</span> <span style="color:#00a8c8">KEY</span> <span style="color:#111">(</span><span style="color:#f92672">`</span><span style="color:#111">tid</span><span style="color:#f92672">`</span><span style="color:#111">)</span> <span style="color:#00a8c8">REFERENCES</span> <span style="color:#f92672">`</span><span style="color:#111">teacher</span><span style="color:#f92672">`</span> <span style="color:#111">(</span><span style="color:#f92672">`</span><span style="color:#111">id</span><span style="color:#f92672">`</span><span style="color:#111">)</span>
</span></span><span style="display:flex;"><span><span style="color:#111">)</span> <span style="color:#111">ENGINE</span><span style="color:#f92672">=</span><span style="color:#111">INNODB</span> <span style="color:#00a8c8">DEFAULT</span> <span style="color:#111">CHARSET</span><span style="color:#f92672">=</span><span style="color:#111">utf8</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">INSERT</span> <span style="color:#00a8c8">INTO</span> <span style="color:#111">student</span><span style="color:#111">(</span><span style="color:#f92672">`</span><span style="color:#111">id</span><span style="color:#f92672">`</span><span style="color:#111">,</span> <span style="color:#f92672">`</span><span style="color:#111">name</span><span style="color:#f92672">`</span><span style="color:#111">,</span> <span style="color:#f92672">`</span><span style="color:#111">tid</span><span style="color:#f92672">`</span><span style="color:#111">)</span> <span style="color:#00a8c8">VALUES</span> <span style="color:#111">(</span><span style="color:#ae81ff">1</span><span style="color:#111">,</span> <span style="color:#d88200">&#39;学1&#39;</span><span style="color:#960050;background-color:#1e0010">，</span> <span style="color:#ae81ff">1</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">INSERT</span> <span style="color:#00a8c8">INTO</span> <span style="color:#111">student</span><span style="color:#111">(</span><span style="color:#f92672">`</span><span style="color:#111">id</span><span style="color:#f92672">`</span><span style="color:#111">,</span> <span style="color:#f92672">`</span><span style="color:#111">name</span><span style="color:#f92672">`</span><span style="color:#111">,</span> <span style="color:#f92672">`</span><span style="color:#111">tid</span><span style="color:#f92672">`</span><span style="color:#111">)</span> <span style="color:#00a8c8">VALUES</span> <span style="color:#111">(</span><span style="color:#ae81ff">2</span><span style="color:#111">,</span> <span style="color:#d88200">&#39;学2&#39;</span><span style="color:#960050;background-color:#1e0010">，</span> <span style="color:#ae81ff">1</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#00a8c8">INSERT</span> <span style="color:#00a8c8">INTO</span> <span style="color:#111">student</span><span style="color:#111">(</span><span style="color:#f92672">`</span><span style="color:#111">id</span><span style="color:#f92672">`</span><span style="color:#111">,</span> <span style="color:#f92672">`</span><span style="color:#111">name</span><span style="color:#f92672">`</span><span style="color:#111">,</span> <span style="color:#f92672">`</span><span style="color:#111">tid</span><span style="color:#f92672">`</span><span style="color:#111">)</span> <span style="color:#00a8c8">VALUES</span> <span style="color:#111">(</span><span style="color:#ae81ff">3</span><span style="color:#111">,</span> <span style="color:#d88200">&#39;学3&#39;</span><span style="color:#960050;background-color:#1e0010">，</span> <span style="color:#ae81ff">1</span><span style="color:#111">);</span>
</span></span></code></pre></div><h3 id="方法一-子查询">方法一 子查询</h3>
<p>Student类里有Teacher属性</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#111">List</span><span style="color:#f92672">&lt;</span><span style="color:#111">Student</span><span style="color:#f92672">&gt;</span> <span style="color:#75af00">getStudent</span><span style="color:#111">();</span>
</span></span><span style="display:flex;"><span><span style="color:#111">Teacher</span> <span style="color:#75af00">getTeacher</span><span style="color:#111">(</span><span style="color:#00a8c8">int</span> <span style="color:#111">id</span><span style="color:#111">);</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;getStudent&#34;</span> <span style="color:#75af00">resultMap=</span><span style="color:#d88200">&#34;StudentTeacher&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  select * from student
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/select&gt;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;resultMap</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;StudentTeacher&#34;</span> <span style="color:#75af00">type=</span><span style="color:#d88200">&#34;Student&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;result</span> <span style="color:#75af00">property=</span><span style="color:#d88200">&#34;id&#34;</span> <span style="color:#75af00">column=</span><span style="color:#d88200">&#34;id&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;result</span> <span style="color:#75af00">property=</span><span style="color:#d88200">&#34;name&#34;</span> <span style="color:#75af00">column=</span><span style="color:#d88200">&#34;name&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;association</span> <span style="color:#75af00">property=</span><span style="color:#d88200">&#34;teacher&#34;</span> <span style="color:#75af00">column=</span><span style="color:#d88200">&#34;tid&#34;</span> <span style="color:#75af00">javaType=</span><span style="color:#d88200">&#34;Teacher&#34;</span> <span style="color:#75af00">select=</span><span style="color:#d88200">&#34;getTeacher&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/resultMap&gt;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;getTeacher&#34;</span> <span style="color:#75af00">resultType=</span><span style="color:#d88200">&#34;Teacher&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  select * from teacher where id = #{tid}
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/select&gt;</span>
</span></span></code></pre></div><h3 id="方法二-连表查询">方法二 连表查询</h3>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;getStudent&#34;</span> <span style="color:#75af00">resultMap=</span><span style="color:#d88200">&#34;StudentTeacher&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  select s.id sid, s.name sname, t.id tid, t.name tname
</span></span><span style="display:flex;"><span>  from student s, teacher t
</span></span><span style="display:flex;"><span>  where s.tid = t.id
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/select&gt;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;resultMap</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;StudentTeacher&#34;</span> <span style="color:#75af00">type=</span><span style="color:#d88200">&#34;Student&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;result</span> <span style="color:#75af00">property=</span><span style="color:#d88200">&#34;id&#34;</span> <span style="color:#75af00">column=</span><span style="color:#d88200">&#34;sid&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;result</span> <span style="color:#75af00">property=</span><span style="color:#d88200">&#34;name&#34;</span> <span style="color:#75af00">column=</span><span style="color:#d88200">&#34;sname&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;association</span> <span style="color:#75af00">property=</span><span style="color:#d88200">&#34;teacher&#34;</span> <span style="color:#75af00">javaType=</span><span style="color:#d88200">&#34;Teacher&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;result</span> <span style="color:#75af00">property=</span><span style="color:#d88200">&#34;id&#34;</span> <span style="color:#75af00">column=</span><span style="color:#d88200">&#34;tid&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;result</span> <span style="color:#75af00">property=</span><span style="color:#d88200">&#34;name&#34;</span> <span style="color:#75af00">column=</span><span style="color:#d88200">&#34;tname&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/association&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/resultMap&gt;</span>
</span></span></code></pre></div><h2 id="一对多">一对多</h2>
<p>Teacher类里有List<code>&lt;Student&gt;</code></p>
<h3 id="方法一-连表查询">方法一 连表查询</h3>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#111">Teacher</span> <span style="color:#75af00">getTeacher</span><span style="color:#111">(</span><span style="color:#00a8c8">int</span> <span style="color:#111">id</span><span style="color:#111">);</span>
</span></span><span style="display:flex;"><span><span style="color:#111">List</span><span style="color:#f92672">&lt;</span><span style="color:#111">Student</span><span style="color:#f92672">&gt;</span> <span style="color:#75af00">getStudentByTeacherId</span><span style="color:#111">(</span><span style="color:#00a8c8">int</span> <span style="color:#111">tid</span><span style="color:#111">);</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;getTeacher&#34;</span> <span style="color:#75af00">resultType=</span><span style="color:#d88200">&#34;TeacherStudent&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  select s.id sid, s.name sname, t.name tname, t.id tid
</span></span><span style="display:flex;"><span>  from student s, teacher t
</span></span><span style="display:flex;"><span>  where s.tid = t.id and t.id = #{id}
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/select&gt;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;resultMap</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;TeacherStudent&#34;</span> <span style="color:#75af00">type=</span><span style="color:#d88200">&#34;Teacher&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;result</span> <span style="color:#75af00">property=</span><span style="color:#d88200">&#34;id&#34;</span> <span style="color:#75af00">column=</span><span style="color:#d88200">&#34;tid&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;result</span> <span style="color:#75af00">property=</span><span style="color:#d88200">&#34;name&#34;</span> <span style="color:#75af00">column=</span><span style="color:#d88200">&#34;tname&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;collection</span> <span style="color:#75af00">property=</span><span style="color:#d88200">&#34;students&#34;</span> <span style="color:#75af00">ofType=</span><span style="color:#d88200">&#34;Student&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;result</span> <span style="color:#75af00">property=</span><span style="color:#d88200">&#34;id&#34;</span> <span style="color:#75af00">column=</span><span style="color:#d88200">&#34;sid&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;result</span> <span style="color:#75af00">property=</span><span style="color:#d88200">&#34;name&#34;</span> <span style="color:#75af00">column=</span><span style="color:#d88200">&#34;sname&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;result</span> <span style="color:#75af00">property=</span><span style="color:#d88200">&#34;tid&#34;</span> <span style="color:#75af00">column=</span><span style="color:#d88200">&#34;tid&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/collection&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/resultMap&gt;</span>
</span></span></code></pre></div><h3 id="方法二-子查询">方法二 子查询</h3>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;getTeacher&#34;</span> <span style="color:#75af00">resultType=</span><span style="color:#d88200">&#34;TeacherStudent&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  select * from teacher where id = #{id}
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/select&gt;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;resultMap</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;TeacherStudent&#34;</span> <span style="color:#75af00">type=</span><span style="color:#d88200">&#34;Teacher&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;result</span> <span style="color:#75af00">property=</span><span style="color:#d88200">&#34;id&#34;</span> <span style="color:#75af00">column=</span><span style="color:#d88200">&#34;id&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;result</span> <span style="color:#75af00">property=</span><span style="color:#d88200">&#34;name&#34;</span> <span style="color:#75af00">column=</span><span style="color:#d88200">&#34;name&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;collection</span> <span style="color:#75af00">property=</span><span style="color:#d88200">&#34;students&#34;</span> <span style="color:#75af00">column=</span><span style="color:#d88200">&#34;id&#34;</span> <span style="color:#75af00">javaType=</span><span style="color:#d88200">&#34;ArrayList&#34;</span> <span style="color:#75af00">ofType=</span><span style="color:#d88200">&#34;Student&#34;</span> <span style="color:#75af00">select=</span><span style="color:#d88200">&#34;getStudentByTeacherId&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/resultMap&gt;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;getStudentByTeacherId&#34;</span> <span style="color:#75af00">resultType=</span><span style="color:#d88200">&#34;Student&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  select * from student where tid = #{tid}
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/select&gt;</span>
</span></span></code></pre></div><h2 id="if">if</h2>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;findActiveBlogWithTitleLike&#34;</span> <span style="color:#75af00">resultType=</span><span style="color:#d88200">&#34;Blog&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  select * from blog
</span></span><span style="display:flex;"><span>  where state = &#39;active&#39;
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;if</span> <span style="color:#75af00">test=</span><span style="color:#d88200">&#34;title != null&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    and title like #{title}
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/if&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;if</span> <span style="color:#75af00">test=</span><span style="color:#d88200">&#34;author != null and author.name != null&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    and author_name like #{author.name}
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/if&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/select&gt;</span>
</span></span></code></pre></div><p>当第一个if不成立，会把成立的第一个if中的and或or自动去掉</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;findActiveBlogWithTitleLike&#34;</span> <span style="color:#75af00">resultType=</span><span style="color:#d88200">&#34;Blog&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  select * from blog
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;where&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;if</span> <span style="color:#75af00">test=</span><span style="color:#d88200">&#34;state != null&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>      state = #{state}
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;/if&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;if</span> <span style="color:#75af00">test=</span><span style="color:#d88200">&#34;title != null&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>      and titile like #{title}
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;/if&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/where&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/select&gt;</span>
</span></span></code></pre></div><h2 id="choose-when-otherwise">choose when otherwise</h2>
<p>类同于switch。同样可以在choose外套where</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;findActiveBlogWithTitleLike&#34;</span> <span style="color:#75af00">resultType=</span><span style="color:#d88200">&#34;Blog&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  select * from blog
</span></span><span style="display:flex;"><span>  where state = &#39;active&#39;
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;choose&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;when</span> <span style="color:#75af00">test=</span><span style="color:#d88200">&#34;title != null&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>      and title like #{title}
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;/when&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;oherwise&gt;</span>
</span></span><span style="display:flex;"><span>      and author_name like #{author.name}
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;/when&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/choose&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/select&gt;</span>
</span></span></code></pre></div><h2 id="set">set</h2>
<p>set会自动删除无关的逗号</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;update</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;updateAuthorIfNecessary&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  update author
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;set&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;if</span> <span style="color:#75af00">test=</span><span style="color:#d88200">&#34;username != null&#34;</span><span style="color:#f92672">&gt;</span>username=#{username},<span style="color:#f92672">&lt;/if&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;if</span> <span style="color:#75af00">test=</span><span style="color:#d88200">&#34;password != null&#34;</span><span style="color:#f92672">&gt;</span>password=#{password},<span style="color:#f92672">&lt;/if&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;if</span> <span style="color:#75af00">test=</span><span style="color:#d88200">&#34;bio != null&#34;</span><span style="color:#f92672">&gt;</span>bio=#{bio}<span style="color:#f92672">&lt;/if&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/set&gt;</span>
</span></span><span style="display:flex;"><span>  where id = #{id}
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/update&gt;</span>
</span></span></code></pre></div><h2 id="trim">trim</h2>
<p>where就是trim的前缀应用，set是后缀应用。就是匹配关键字，在配置前/后缀替换</p>
<h2 id="sql片段">sql片段</h2>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;sql</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;if-x&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;if</span> <span style="color:#75af00">test=</span><span style="color:#d88200">&#34;username != null&#34;</span><span style="color:#f92672">&gt;</span>username=#{username},<span style="color:#f92672">&lt;/if&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;if</span> <span style="color:#75af00">test=</span><span style="color:#d88200">&#34;password != null&#34;</span><span style="color:#f92672">&gt;</span>password=#{password},<span style="color:#f92672">&lt;/if&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;if</span> <span style="color:#75af00">test=</span><span style="color:#d88200">&#34;bio != null&#34;</span><span style="color:#f92672">&gt;</span>bio=#{bio}<span style="color:#f92672">&lt;/if&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/sql&gt;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;update</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;updateAuthorIfNecessary&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  update author
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;set&gt;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">&lt;include</span> <span style="color:#75af00">refid=</span><span style="color:#d88200">&#34;if-x&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/set&gt;</span>
</span></span><span style="display:flex;"><span>  where id = #{id}
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/update&gt;</span>
</span></span></code></pre></div><h2 id="foreach">foreach</h2>
<p>collection的值是入参对象属性名</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;select</span> <span style="color:#75af00">id=</span><span style="color:#d88200">&#34;selectPostIn&#34;</span> <span style="color:#75af00">resultType=</span><span style="color:#d88200">&#34;Post&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>  select *
</span></span><span style="display:flex;"><span>  from post
</span></span><span style="display:flex;"><span>  where id in
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;foreach</span> <span style="color:#75af00">item=</span><span style="color:#d88200">&#34;item&#34;</span> <span style="color:#75af00">index=</span><span style="color:#d88200">&#34;index&#34;</span> <span style="color:#75af00">collection=</span><span style="color:#d88200">&#34;list&#34;</span> <span style="color:#75af00">open=</span><span style="color:#d88200">&#34;(&#34;</span> <span style="color:#75af00">separator=</span><span style="color:#d88200">&#34;,&#34;</span> <span style="color:#75af00">close=</span><span style="color:#d88200">&#34;)&#34;</span><span style="color:#f92672">&gt;</span>
</span></span><span style="display:flex;"><span>    #{item}
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">&lt;/foreach&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;/update&gt;</span>
</span></span></code></pre></div><h2 id="缓存">缓存</h2>
<p>默认定义了两级缓存，且只开启了一级缓存（SqlSession级别的缓存，也称为本地缓存），也就同一个SqlSession的select结果会被缓存，insert、update、delete会刷新缓存</p>
<p>sqlSession.clearCache(); // 手动清理缓存</p>
<p>二级缓存需要手动开启，基于namespace级别的缓存，加一行<code>&lt;cache/&gt;</code>，或者可以对它进行配置，如</p>
<div class="highlight"><pre tabindex="0" style="color:#272822;background-color:#fafafa;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;cache</span>
</span></span><span style="display:flex;"><span> <span style="color:#75af00">eviction=</span><span style="color:#d88200">&#34;FIFO&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#75af00">flushInterval=</span><span style="color:#d88200">&#34;60000&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#75af00">size=</span><span style="color:#d88200">&#34;512&#34;</span>
</span></span><span style="display:flex;"><span> <span style="color:#75af00">readOnly=</span><span style="color:#d88200">&#34;true&#34;</span> <span style="color:#f92672">/&gt;</span>
</span></span></code></pre></div><p>存入二级缓存的时机在sqlSession.close();之后</p>
<p>定义了缓存接口Cache，可以实现该接口来自定义二级缓存</p>
<p>缓存清除策略有 LRU（移除最长不被用的对象，默认）、FIFO（按顺序移除）、SOFT（基于垃圾回收器状态和软引用规则）、WEAK（更积极地基于垃圾收集器状态和弱引用规则）</p>


    


                    
                    <div class="row"></div> 

                </div>

            </div> 

        </div> 
<script src="/lib/jquery.min.js"></script> 
<script src="/lib/popper.min.js"></script> 

<script src="/js/bootstrap.min.js"></script> 


<script type="text/javascript" src="/plugins/lunr.min.js"></script>
<script type="text/javascript" src="/plugins/auto-complete.js"></script>
<link href="/plugins/auto-complete.css" rel="stylesheet">
<script type="text/javascript">
  
      var baseurl = "\/";
  
</script>
<script type="text/javascript" src="/plugins/search.js"></script>

<script type="text/javascript" src="/plugins/favorites.js"></script>


<script type="text/javascript" src="/plugins/clipboard.js"></script>
<script>
  new ClipboardJS('.btn');
</script>
</body>
</html>
